<?php

	class NewAction extends BackAction{

		public function index(){
		$db=D('New_article');
		$where='';
		S('new_article',null);
		
		S('new_article',$new_article);
		$count=$db->where($where)->count();
		$page=new Page($count,25);
		$info=$db->where($where)->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('info',$info);
		$this->assign('page',$page->show());
		$this->display();
	}
		public function server(){	
			$data = array();
			if(empty($_POST['content'])||empty($_POST['title'])){
				$this->error("内容不完整");		
			}
			
			$data['content'] = $_POST['content'];
			$data['title'] = $_POST['title'];
			$data['classid'] = 1;//--这个也许就是他们之前想要给新闻分的类 因为只有一个类 所以默认为1
            $data['type'] = isset($_POST['type'])?$_POST['type']:1; //--但是我这里不用那个分类 为了防止万一
			$data['createtime'] = time();
			$article = M('New_article')->add($data);
			if($article){
				$this->success("更新成功");
			}	
		}
		
		public function manager(){
			$article = M('New_article');
			$count = $article->count("id");
			$page = new Page($count,2);
			$page->setConfig('theme','%upPage%%downPage%%first%%prePage%%linkPage%%nextPage%%end%');
			$show = $page->show();
			$list =$article->order('createtime desc')->limit($page->firstRow.','.$page->listRows)->field('id,title,createtime,updatetime')->select();
			$this->assign('info',$list);
			$this->assign('show',$show);
			$this->display();
		}
		
		//--更新编辑页面
		public function edit(){
			$id = intval($_GET['id']);
			if(isset($id)){
				$article = M('New_article');
				$news = $article->where('id='.$id)->field('id,title,content,type')->find();
				$this->assign('news',$news);
				$this->display();
			}	
		}
		
		//--更新处理函数
		public function edit_server(){
			$id=intval($_POST['id']);
			$title = $_POST['title'];
			$content = $_POST['content'];
			if(isset($id)&&isset($title)&&isset($content)){
				$data = array();
				$data['id'] = $id;
				$data['title'] = $title;
				$data['content'] = $content;
				$data['updatetime']=time();
				$res = M('New_article')->save($data);	
				if(false===$res){
					$this->error("更新失败");
				}else{
					$this->success("更新成功");
				}
			}else{
				$this->error("内容不完整");
			}
		}
		
		public function del(){
			$id=intval($_GET['id']);
			if(isset($id)){
				$res = M('New_article')->where('id='.$id)->delete();
				if(false===$res){
					$this->error("删除失败");
				}else{
					$this->success('删除成功');
				}	
			}
			else{
				$this->error("操作失败");
			}
		}
		
		
		
		//--输出js调用  配合upload函数 弹出信息
		private function mkhtml($fn,$fileurl,$message)
		{
			$str='<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$fn.', \''.$fileurl.'\', \''.$message.'\');</script>';
			exit($str);
		}
		
		//--配合upload函数 
		private function uploadfile($config)
		{
			//global $config;
			//判断是否是非法调用
			echo "uploadfile";
			if(empty($_GET['CKEditorFuncNum']))
				$this->mkhtml(1,"","错误的功能调用请求");
			$fn=$_GET['CKEditorFuncNum'];
			if(!in_array($_GET['type'],$config['type']))
				$this->mkhtml(1,"","错误的文件调用请求");
			$type=$_GET['type'];
			if(is_uploaded_file($_FILES['upload']['tmp_name']))
			{
				//判断上传文件是否允许
				$filearr=pathinfo($_FILES['upload']['name']);
				$filetype=$filearr["extension"];
				if(!in_array($filetype,$config[$type]))
					$this->mkhtml($fn,"","错误的文件类型！");
				//判断文件大小是否符合要求
				if($_FILES['upload']['size']>$config[$type."_size"]*1024)
					$this->mkhtml($fn,"","上传的文件不能超过".$config[$type."_size"]."KB！");
				//$filearr=explode(".",$_FILES['upload']['name']);
				//$filetype=$filearr[count($filearr)-1];
				$file_abso=$config[$type."_dir"]."/".$config['name'].".".$filetype;
				$file_host=$_SERVER['DOCUMENT_ROOT'].$file_abso;
					
				if(move_uploaded_file($_FILES['upload']['tmp_name'],$file_host))
				{
					$this->mkhtml($fn,$config['site_url'].$file_abso,$config['message']);
				}
				else
				{
					$this->mkhtml($fn,"","文件上传失败，请检查上传目录设置和目录读写权限");
				}
			}
		}
		
		//--图片上传功能  编辑器点击上传功能的时候调用这个函数 这功能还需要配置 ckeditor的config.js
		public function upload(){
			$config=array();
			$config['type']=array("flash","img"); //上传允许type值
			$config['img']=array("jpg","bmp","gif"); //img允许后缀
			$config['flash']=array("flv","swf"); //flash允许后缀
			$config['flash_size']=200; //上传flash大小上限 单位：KB
			$config['img_size']=500; //上传img大小上限 单位：KB
			$config['message']="上传成功"; //上传成功后显示的消息，若为空则不显示
			$config['name']=mktime(); //上传后的文件命名规则 这里以unix时间戳来命名
			$config['flash_dir']="/vjbUpload/flash";
			//上传flash文件地址 采用绝对地址 方便upload.php文件放在站内的任何位置 后面不加"/"
			$config['img_dir']="/vjbUpload/img";
			//上传img文件地址 采用绝对地址 采用绝对地址 方便upload.php文件放在站内的任何位置 后面不加"/"
			$config['site_url']=""; //网站的网址 这与图片上传后的地址有关 最后不加"/" 可留空
			//文件上传
			//--$fn=$_GET['CKEditorFuncNum'];
			//--$this->mkhtml($fn,"","测试");
			
			$this->uploadfile($config);
		}
		
		
	}